package com.huawei.hwid.core.d;

import android.content.Context;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.ReadWhiteListInfo;
import com.huawei.hwid.core.datatype.SiteDefaultInfo;
import com.huawei.hwid.core.datatype.SiteListInfo;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class j {
    private static SiteListInfo a = null;

    public static String a(Context context, int i) {
        InputStreamReader inputStreamReader;
        Object e;
        BufferedReader bufferedReader;
        Throwable th;
        if (i == 1) {
            try {
                inputStreamReader = new InputStreamReader(context.getResources().getAssets().open("global_cfg_for_android_mobile.xml"), "UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e = e2;
                bufferedReader = null;
                inputStreamReader = null;
                try {
                    e.c("IpCountryUtil", "UnsupportedEncodingException" + e.getClass().getSimpleName(), true);
                    a(inputStreamReader, bufferedReader, null);
                    a(inputStreamReader, bufferedReader, null);
                    return "";
                } catch (Throwable th2) {
                    th = th2;
                    a(inputStreamReader, bufferedReader, null);
                    throw th;
                }
            } catch (IOException e3) {
                e = e3;
                bufferedReader = null;
                inputStreamReader = null;
                e.c("IpCountryUtil", "IOException " + e.getClass().getSimpleName(), true);
                a(inputStreamReader, bufferedReader, null);
                a(inputStreamReader, bufferedReader, null);
                return "";
            } catch (Throwable th3) {
                th = th3;
                bufferedReader = null;
                inputStreamReader = null;
                a(inputStreamReader, bufferedReader, null);
                throw th;
            }
        } else if (i == 2) {
            inputStreamReader = new InputStreamReader(context.getResources().getAssets().open("globalCityList.xml"), "UTF-8");
        } else if (i == 3) {
            inputStreamReader = new InputStreamReader(context.getResources().getAssets().open("huaweiservicelist.xml"), "UTF-8");
        } else {
            e.b("IpCountryUtil", "other", true);
            inputStreamReader = null;
        }
        if (inputStreamReader == null) {
            try {
                String str = "";
                a(inputStreamReader, null, null);
                return str;
            } catch (UnsupportedEncodingException e4) {
                e = e4;
                bufferedReader = null;
                e.c("IpCountryUtil", "UnsupportedEncodingException" + e.getClass().getSimpleName(), true);
                a(inputStreamReader, bufferedReader, null);
                a(inputStreamReader, bufferedReader, null);
                return "";
            } catch (IOException e5) {
                e = e5;
                bufferedReader = null;
                e.c("IpCountryUtil", "IOException " + e.getClass().getSimpleName(), true);
                a(inputStreamReader, bufferedReader, null);
                a(inputStreamReader, bufferedReader, null);
                return "";
            } catch (Throwable th4) {
                th = th4;
                bufferedReader = null;
                a(inputStreamReader, bufferedReader, null);
                throw th;
            }
        }
        bufferedReader = new BufferedReader(inputStreamReader);
        try {
            str = "";
            StringBuilder stringBuilder = new StringBuilder();
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    stringBuilder.append(readLine);
                } else {
                    str = stringBuilder.toString();
                    a(inputStreamReader, bufferedReader, null);
                    return str;
                }
            }
        } catch (UnsupportedEncodingException e6) {
            e = e6;
            e.c("IpCountryUtil", "UnsupportedEncodingException" + e.getClass().getSimpleName(), true);
            a(inputStreamReader, bufferedReader, null);
            a(inputStreamReader, bufferedReader, null);
            return "";
        } catch (IOException e7) {
            e = e7;
            e.c("IpCountryUtil", "IOException " + e.getClass().getSimpleName(), true);
            a(inputStreamReader, bufferedReader, null);
            a(inputStreamReader, bufferedReader, null);
            return "";
        }
    }

    private static void a(InputStreamReader inputStreamReader, BufferedReader bufferedReader, InputStream inputStream) {
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.c("IpCountryUtil", "IOException" + e.getClass().getSimpleName(), true);
            }
        }
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e2) {
                e.b("IpCountryUtil", "IOException" + e2.getClass().getSimpleName(), true);
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e22) {
                e.c("IpCountryUtil", "IOException" + e22.getClass().getSimpleName(), true);
            }
        }
    }

    public static String b(Context context, int i) {
        InputStream fileInputStream;
        Object e;
        InputStream inputStream;
        InputStreamReader inputStreamReader;
        Throwable th;
        InputStreamReader inputStreamReader2;
        BufferedReader bufferedReader = null;
        try {
            String str = context.getFilesDir().getAbsolutePath() + "/";
            if (i == 1) {
                fileInputStream = new FileInputStream(str + "global_cfg_for_android_mobile.xml");
            } else if (i == 2) {
                fileInputStream = new FileInputStream(str + "globalCityList.xml");
            } else if (i == 3) {
                fileInputStream = new FileInputStream(str + "huaweiservicelist.xml");
            } else {
                e.b("IpCountryUtil", "other", true);
                fileInputStream = null;
            }
            if (fileInputStream == null) {
                try {
                    str = "";
                    a(null, null, fileInputStream);
                    return str;
                } catch (UnsupportedEncodingException e2) {
                    e = e2;
                    inputStream = fileInputStream;
                    inputStreamReader = null;
                    try {
                        e.b("IpCountryUtil", "getFromDataFile UnsupportedEncodingException " + e.getClass().getSimpleName(), true);
                        a(inputStreamReader, bufferedReader, inputStream);
                        a(inputStreamReader, bufferedReader, inputStream);
                        return "";
                    } catch (Throwable th2) {
                        th = th2;
                        inputStreamReader2 = inputStreamReader;
                        fileInputStream = inputStream;
                        a(inputStreamReader2, bufferedReader, fileInputStream);
                        throw th;
                    }
                } catch (IOException e3) {
                    e = e3;
                    inputStreamReader2 = null;
                    try {
                        e.b("IpCountryUtil", "getFromDataFile IOException " + e.getClass().getSimpleName(), true);
                        a(inputStreamReader2, bufferedReader, fileInputStream);
                        a(inputStreamReader2, bufferedReader, fileInputStream);
                        return "";
                    } catch (Throwable th3) {
                        th = th3;
                        a(inputStreamReader2, bufferedReader, fileInputStream);
                        throw th;
                    }
                } catch (Throwable th4) {
                    th = th4;
                    inputStreamReader2 = null;
                    a(inputStreamReader2, bufferedReader, fileInputStream);
                    throw th;
                }
            }
            inputStreamReader2 = new InputStreamReader(fileInputStream, "UTF-8");
            try {
                BufferedReader bufferedReader2 = new BufferedReader(inputStreamReader2);
                try {
                    str = "";
                    StringBuilder stringBuilder = new StringBuilder();
                    while (true) {
                        String readLine = bufferedReader2.readLine();
                        if (readLine != null) {
                            stringBuilder.append(readLine);
                        } else {
                            str = stringBuilder.toString();
                            a(inputStreamReader2, bufferedReader2, fileInputStream);
                            return str;
                        }
                    }
                } catch (UnsupportedEncodingException e4) {
                    e = e4;
                    bufferedReader = bufferedReader2;
                    inputStream = fileInputStream;
                    inputStreamReader = inputStreamReader2;
                    e.b("IpCountryUtil", "getFromDataFile UnsupportedEncodingException " + e.getClass().getSimpleName(), true);
                    a(inputStreamReader, bufferedReader, inputStream);
                    a(inputStreamReader, bufferedReader, inputStream);
                    return "";
                } catch (IOException e5) {
                    e = e5;
                    bufferedReader = bufferedReader2;
                    e.b("IpCountryUtil", "getFromDataFile IOException " + e.getClass().getSimpleName(), true);
                    a(inputStreamReader2, bufferedReader, fileInputStream);
                    a(inputStreamReader2, bufferedReader, fileInputStream);
                    return "";
                } catch (Throwable th5) {
                    th = th5;
                    bufferedReader = bufferedReader2;
                    a(inputStreamReader2, bufferedReader, fileInputStream);
                    throw th;
                }
            } catch (UnsupportedEncodingException e6) {
                e = e6;
                inputStream = fileInputStream;
                inputStreamReader = inputStreamReader2;
                e.b("IpCountryUtil", "getFromDataFile UnsupportedEncodingException " + e.getClass().getSimpleName(), true);
                a(inputStreamReader, bufferedReader, inputStream);
                a(inputStreamReader, bufferedReader, inputStream);
                return "";
            } catch (IOException e7) {
                e = e7;
                e.b("IpCountryUtil", "getFromDataFile IOException " + e.getClass().getSimpleName(), true);
                a(inputStreamReader2, bufferedReader, fileInputStream);
                a(inputStreamReader2, bufferedReader, fileInputStream);
                return "";
            }
        } catch (UnsupportedEncodingException e8) {
            e = e8;
            inputStream = null;
            inputStreamReader = null;
            e.b("IpCountryUtil", "getFromDataFile UnsupportedEncodingException " + e.getClass().getSimpleName(), true);
            a(inputStreamReader, bufferedReader, inputStream);
            a(inputStreamReader, bufferedReader, inputStream);
            return "";
        } catch (IOException e9) {
            e = e9;
            fileInputStream = null;
            inputStreamReader2 = null;
            e.b("IpCountryUtil", "getFromDataFile IOException " + e.getClass().getSimpleName(), true);
            a(inputStreamReader2, bufferedReader, fileInputStream);
            a(inputStreamReader2, bufferedReader, fileInputStream);
            return "";
        } catch (Throwable th6) {
            th = th6;
            fileInputStream = null;
            inputStreamReader2 = null;
            a(inputStreamReader2, bufferedReader, fileInputStream);
            throw th;
        }
    }

    private static void a(Context context, XmlPullParser xmlPullParser, List<SiteListInfo> list, SiteDefaultInfo siteDefaultInfo, ArrayList<String> arrayList, String str) {
        if ("site".equals(str)) {
            a = new SiteListInfo();
            SiteListInfo.a(context, xmlPullParser, a, str);
            list.add(a);
        } else if ("site-list".equals(str)) {
            SiteDefaultInfo.a(xmlPullParser, siteDefaultInfo);
        } else if (HianalyticsData.DOMAIN.equals(str)) {
            ReadWhiteListInfo.a(xmlPullParser, arrayList, str);
        }
    }

    private static XmlPullParser b(Context context, String str, String str2) throws XmlPullParserException, IOException {
        int a = a(context, str);
        int a2 = a(context, str2);
        e.d("IpCountryUtil", "parseSiteCountryXML xmlDataFileCode " + a, true);
        e.d("IpCountryUtil", "parseSiteCountryXML xmlAssetsFileCode " + a2, true);
        if (a < a2) {
            e.d("IpCountryUtil", "parseSiteCountryXML xmlDataFileCode < xmlAssetsFileCode", true);
            return p.a(str2.getBytes("UTF-8"));
        }
        e.d("IpCountryUtil", "parseSiteCountryXML else ", true);
        return p.a(str.getBytes("UTF-8"));
    }

    public static void a(Context context, List<SiteListInfo> list, SiteDefaultInfo siteDefaultInfo, ArrayList<String> arrayList) throws XmlPullParserException, IOException {
        if (context == null) {
            e.c("IpCountryUtil", "parseSiteCountryXML mContext == null", true);
            return;
        }
        e.b("IpCountryUtil", "getFromDataFile", true);
        a = null;
        XmlPullParser b = b(context, b(context, 1), a(context, 1));
        for (int eventType = b.getEventType(); 1 != eventType; eventType = b.next()) {
            String name = b.getName();
            switch (eventType) {
                case 2:
                    a(context, b, list, siteDefaultInfo, arrayList, name);
                    break;
                default:
                    break;
            }
        }
    }

    private static int a(Context context, String str) {
        return g.c(context, str);
    }

    public static boolean a(Context context, String str, String str2) {
        FileOutputStream fileOutputStream = null;
        try {
            context.deleteFile(str);
            fileOutputStream = context.openFileOutput(str, 0);
            fileOutputStream.write(str2.getBytes("UTF-8"));
            fileOutputStream.close();
            if (fileOutputStream == null) {
                return true;
            }
            try {
                fileOutputStream.close();
                return true;
            } catch (IOException e) {
                e.d("IpCountryUtil", "IOException", true);
                return true;
            }
        } catch (FileNotFoundException e2) {
            e.d("IpCountryUtil", "writeSMSAvailableCountryXML FileNotFoundException", true);
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e3) {
                    e.d("IpCountryUtil", "IOException", true);
                }
            }
            return false;
        } catch (IOException e4) {
            e.d("IpCountryUtil", "writeSMSAvailableCountryXML IOException", true);
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e5) {
                    e.d("IpCountryUtil", "IOException", true);
                }
            }
            return false;
        } catch (Throwable th) {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e6) {
                    e.d("IpCountryUtil", "IOException", true);
                }
            }
        }
    }
}
